﻿
namespace SeedObject
{
    using Serializable = System.SerializableAttribute;
    using XmlIgnoreAttribute = System.Xml.Serialization.XmlIgnoreAttribute;
    using DbType = System.Data.DbType;

    [Serializable]
    internal class SimpleValue : Func<string, Field>
    {
        public override string Run()
        {
            return Result = Context.ToString();
        }
    }
    [Serializable]
    public class Field : Conteiner<Validation<Field>>, IFireEvent
    {
        #region Constructor
            public Field()
                : base()
            {
                SetGender(Gender.Male);
                SetNumber(Number.Singular);
            }
        #endregion
        #region Properties
            private Actions<Field> _Actions;
            /// <summary>
            /// 
            /// </summary>   
            public Actions<Field> Actions
            {
                get
                {
                    return _Actions;
                }
                set
                {
                    _Actions = value;
                    _Actions.Parent = this;
                    _Actions.ForEach(
                        delegate(Action<Field> action)
                        {
                            action.Context = this;
                        }
                    );
                }
            }
            //
            private DbType _DBType;
            /// <summary>
            /// 
            /// </summary>  
            public DbType DBType
            {
                get 
                { 
                    return _DBType; 
                }
                set
                {
                    SaveAttribute(ID.DbType, _DBType, 
                        delegate() 
                        { 
                            _DBType = (DbType)Attributes[ID.DbType]; 
                        }
                    );
                    _DBType = value;
                }

            }

            private string _Format;
            /// <summary>
            /// <c>formato : </c> propiedad formato.
            /// </summary>  
            public string Format
            {
                get 
                { 
                    return _Format; 
                }
                set
                {
                    SaveAttribute(ID.Format, _Format, 
                        delegate() 
                        { 
                            _Format = (string)Attributes[ID.Format]; 
                        }
                    );
                    _Format = value;
                }

            }
            //
            private string _Help;
            /// <summary>
            /// 
            /// </summary>  
            public string Help
            {
                get { return _Help; }
                set
                {
                    SaveAttribute(ID.Help, _Help,
                        delegate()
                        {
                            _Help = (string)Attributes[ID.Help];
                        }
                    );
                    _Help = value;
                }

            }
            //
            private string _Label;
            /// <summary>
            /// 
            /// </summary>  
            public string Label
            {
                get 
                { 
                    return _Label; 
                }
                set
                {
                    SaveAttribute(ID.Label, _Label, 
                        delegate() 
                        {
                            _Label = (string)Attributes[ID.Label];
                        }
                    );
                    _Label = value;
                }

            }
            //
            private string _Length;
            /// <summary>
            ///
            /// </summary>  
            public string Length
            {
                get 
                { 
                    return _Length; 
                }
                set
                {
                    SaveAttribute(ID.Length, _Length, 
                        delegate()
                        {
                            _Length = (string)Attributes[ID.Length]; 
                        }
                    );
                    _Length = value;
                }
            }

            private string _Name;
            /// <summary>
            /// 
            /// </summary>
            public string Name
            {
                get { return _Name; }
                set
                {
                    SaveAttribute(ID.Name, _Name, 
                        delegate() 
                        { 
                            _Name = (string)Attributes[ID.Name]; 
                        }
                    );
                    _Name = value;
                }

            }
            //
            private string _Operator = Chains.Equal;
            /// <summary>
            /// 
            /// </summary>  
            public string Operator
            {
                get 
                { 
                    return _Operator; 
                }
                set
                {
                    SaveAttribute(ID.Operator, _Operator, 
                        delegate() 
                        { 
                            _Operator = (string)Attributes[ID.Operator]; 
                        }
                    );
                    _Operator = value;
                }
            }
            //
            private bool _PrimaryKey;
            /// <summary>
            /// 
            /// </summary>  
            public bool PrimaryKey
            {
                get 
                { 
                    return _PrimaryKey; 
                }
                set
                {
                    SaveAttribute(ID.PrimaryKey, _PrimaryKey, 
                        delegate() 
                        { 
                            _PrimaryKey = (bool)Attributes[ID.PrimaryKey]; 
                        }
                    );
                    _PrimaryKey = value;
                }

            }
            //
            private bool _Sequence;
            /// <summary>
            /// 
            /// </summary>  
            public bool Sequence
            {
                get 
                { 
                    return _Sequence; 
                }
                set
                {
                    SaveAttribute(ID.Sequence, _Sequence, 
                        delegate() 
                        { 
                            _Sequence = (bool)Attributes[ID.Sequence]; 
                        }
                    );
                    _Sequence = value;
                }

            }
            //
            private bool _SqlExpression;
            /// <summary>
            /// 
            /// </summary>  
            public bool SqlExpression
            {
                get { return _SqlExpression; }
                set
                {
                    SaveAttribute(ID.SqlExpression, _SqlExpression, 
                        delegate() 
                        { 
                            _SqlExpression = (bool)Attributes[ID.SqlExpression]; 
                        }
                    );
                    _SqlExpression = value;
                }

            }
            //
            private string _Table;
            /// <summary>
            /// 
            /// </summary>  
            public string Table
            {
                get 
                { 
                    return _Table; 
                }
                set
                {
                    SaveAttribute(ID.Table, _Table, 
                        delegate() 
                        { 
                            _Table = (string)Attributes[ID.Table]; 
                        }
                    );
                    _Table = value;
                }
            }
            //
            private string _Translator;
            /// <summary>
            /// <c>traductor : </c> propiedad traductor.
            /// </summary>  
            public string Translator
            {
                get 
                {
                    return _Translator; 
                }
                set
                {
                    SaveAttribute(ID.Translator, _Translator,
                        delegate() 
                        { 
                            _Translator = (string)Attributes[ID.Translator]; 
                        }
                    );
                    _Translator = value;
                }

            }
            //
            private bool _Unique;
            /// <summary>
            /// 
            /// </summary>  
            public bool Unique
            {
                get 
                {
                    return _Unique; 
                }
                set
                {
                    SaveAttribute(ID.Unique, _Unique, 
                        delegate() 
                        {
                            _Unique = (bool)Attributes[ID.Unique]; 
                        }
                    );
                    _Unique = value;
                }

            }

            private string _Value;
            internal Func<string, Field> _value;
            /// <summary>
            /// 
            /// </summary>  
            public string Value
            {
                get
                {
                    return _value.Run();
                }
                set
                {
                    if (value != null)
                    {
                        SaveAttribute(ID.Value, _Value,
                            delegate()
                            {
                                _Value = (string)Attributes[ID.Value];
                                Run(Trigger.OnChange);
                            }
                         );
                        _Value = value;
                    }
                    Run(Trigger.OnChange);
                }
            }

            private Conteiner<Func<string,Field>> _Values;
            /// <summary>
            /// 
            /// </summary>
            public Conteiner<Func<string, Field>> Values
            {
                get { return _Values; }
                set { _Values = value; }
            }
        #endregion


        #region IFireEvent Members

            public void Run(Trigger trigger)
            {
                if (_Actions == null) 
                    return;
                _Actions.Run(trigger);
            }

        #endregion
    }

}
